<?php

Yii::import( "application.controllers.FrontendBaseController" );

class MessageController extends FrontendBaseController
{
    public function actionIndex()
    {
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }  
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Сообщения" => "" );
        
        $this->redirect('/message/inbox');
        
    }
    
    public function actionInbox()
    {
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }  
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Сообщения" => "/message",
                                    "Входящие" => '');
        
        $inbox = MessageFacade::getUserInboxMails(Yii::app()->user->id);
        
        $this->render('messages', array(
            'mails' => $inbox,
            'mode'  => 'inbox'
            )
        );
    }
    
    public function actionOutbox()
    {
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }  
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Сообщения" => "/message",
                                    "Исходящие" => '');
        
        $outbox = MessageFacade::getUserOutboxMails(Yii::app()->user->id);
        
        $this->render('messages', array(
            'mails' => $outbox,
            'mode'  => 'outbox'
            )
        );
    }
    
    public function actionDelete( $id )
    {
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }                
        
        $message = MessageFacade::getMessageByPk($id);
        if ($message === null)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        
        if (($message->from_user == Yii::app()->user->id) || ($message->to_user == Yii::app()->user->id))
        {
            $message->delete();
        }
        else
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        
        $this->redirect("/message");
    }
    
    public function actionSend( $id )
    {
        if (Yii::app()->user->isGuest)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }        
        $user = $this->loadModel($id);
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Сообщения" => "/message",
                                    "Отправка сообщения" => '');
                
        if (!$_POST['Message'])
        {
            $model = new Message();
            $model->to_user = $user->user_id;
            $this->render('send', array(
                'model' => $model
                )
            );
        }
        else
        {
            $message = new Message();
            $message->attributes = $_POST['Message'];
            $message->from_user = Yii::app()->user->id;
            $message->to_user = $user->user_id;
            $message->save();
            
            $this->render('send', array(
                'sended' => true
                )
            );
        }
    }
    
    public function loadModel( $id )
    {
        $model = UserFacade::GetUserById( $id );
        if ( $model === null )
            throw new CHttpException( 404, 'The requested page does not exist.' );
        return $model;
    }   
}
?>
